package com.suyang.syt.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suyang.syt.context.BeanFactoryContext;
import com.suyang.syt.dao.interfaces.WeixinMaterialDao;
import com.suyang.syt.dao.interfaces.WeixinMsgImageTextDao;
import com.suyang.syt.model.WeixinMaterial;
import com.suyang.syt.model.WeixinMsgImageText;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.PagerEasyUI;
import com.suyang.syt.service.interfaces.WeixinMaterialService;
import com.suyang.syt.util.EmptyUtil;
import com.suyang.syt.util.JsonUtil;
import com.suyang.syt.weixin.WeixinKit;
import com.suyang.syt.weixin.WeixinMessage;

@Service
public class WeixinMaterialServiceImpl implements WeixinMaterialService {

	@Autowired
	private WeixinMaterialDao weixinMaterialDao;
	@Autowired
	private WeixinMsgImageTextDao weixinMsgImageTextDao;

	@Override
	public AjaxObj add(WeixinMaterial weixinMaterial) throws Exception {
		if (weixinMaterial != null) {
			String type = weixinMaterial.getType();
			int status = weixinMaterial.getStatus();
			if (EmptyUtil.isEmpty(type)) {
				return new AjaxObj(0, "素材类型不能为空");
			}
			if (WeixinKit.MSG_NEWS.equals(type)) {
				// 图文素材
				if (status == 0) {
					return new AjaxObj(0, "临时素材暂不支持图文");
				}
				if (EmptyUtil.isEmpty(weixinMaterial.getWeixinImageTextIds())) {
					return new AjaxObj(0, "图文信息不能为空");
				}
				List<WeixinMsgImageText> list = weixinMsgImageTextDao.listInIds(weixinMaterial.getWeixinImageTextIds());
				if (EmptyUtil.isNotEmpty(list)) {
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("articles", list);
					String content = WeixinKit.uploadNews(JsonUtil.toJson(map));
					if (EmptyUtil.isNotEmpty(content)) {
						weixinMaterial.setMedia_id(content);
						weixinMaterialDao.add(weixinMaterial);
						return new AjaxObj(1, "永久素材(" + type + ")创建成功!");
					} else {
						return new AjaxObj(0, "永久素材(" + type + ")创建失败!");
					}
				}
			} else {
				String media = weixinMaterial.getMedia();
				if (EmptyUtil.isEmpty(media)) {
					return new AjaxObj(0, "上传的素材不能为空!");
				}
				String path = BeanFactoryContext.getRealPath() + "/resources/upload/weixin/" + media;
				if (status == 1) {
					// 永久素材(不含图文)
					WeixinMessage message = null;
					if (WeixinKit.MSG_VIDEO.equals(type)) {
						// 永久视频素材
						String title = weixinMaterial.getTitle();
						String introduction = weixinMaterial.getIntroduction();
						if (EmptyUtil.isEmpty(title) || EmptyUtil.isEmpty(introduction)) {
							return new AjaxObj(0, "永久视频素材必须填写标题和描述信息!");
						}
						Map<String, String> map = new HashMap<String, String>();
						map.put("title", title);
						map.put("introduction", introduction);
						message = WeixinKit.uploadMaterialVideo(path, JsonUtil.toJson(map));
					} else {
						// 其他类型永久素材
						message = WeixinKit.uploadMaterial(path, type);
					}
					if (message != null && EmptyUtil.isNotEmpty(message.getMedia_id())) {
						weixinMaterial.setMedia_id(message.getMedia_id());
						weixinMaterial.setMedia_url(message.getUrl());
						weixinMaterialDao.add(weixinMaterial);
						return new AjaxObj(1, "永久素材(" + type + ")创建成功!");
					} else {
						return new AjaxObj(0, "永久素材(" + type + ")创建失败!");
					}
				} else {
					// 临时素材
					String content = WeixinKit.uploadMedia(path, type);
					if (EmptyUtil.isNotEmpty(content)) {
						weixinMaterial.setMedia_id(content);
						weixinMaterialDao.add(weixinMaterial);
						return new AjaxObj(1, "临时素材(" + type + ")创建成功!");
					} else {
						return new AjaxObj(0, "临时素材(" + type + ")创建失败!");
					}
				}
			}
		}
		return new AjaxObj(0, "素材添加失败!");
	}

	@Override
	public AjaxObj deletes(String ids) throws Exception {
		if (EmptyUtil.isNotEmpty(ids)) {
			weixinMaterialDao.deletes(ids);
			return new AjaxObj(1, "删除成功！");
		} else {
			return new AjaxObj(0, "请选择要删除的记录！");
		}
	}

	@Override
	public PagerEasyUI<WeixinMaterial> listByEasyUI(int page, int rows, String sort, String order, String createDate1, String createDate2, String type, String status) throws Exception {
		// 微信临时素材3天后失效 3*24*3600*1000=259200000
		String time = (System.currentTimeMillis() - 259200000) + "";
		StringBuilder hql = new StringBuilder(" from WeixinMaterial wm where wm.created_at > ");
		hql.append(time);
		if (EmptyUtil.isNotEmpty(createDate1)) {
			hql.append(" and wm.createTime >= '").append(createDate1).append("' ");
		}
		if (EmptyUtil.isNotEmpty(createDate2)) {
			hql.append(" and wm.createTime <= '").append(createDate2).append("' ");
		}
		if (EmptyUtil.isNotEmpty(status)) {
			hql.append(" and wm.status = 1");
		}
		if (EmptyUtil.isNotEmpty(type)) {
			hql.append(" and wm.type =").append("'").append(type).append("'");
		}
		if (EmptyUtil.isNotEmpty(sort)) {
			hql.append(" order by wm.").append(sort);
		}
		if (EmptyUtil.isNotEmpty(order)) {
			hql.append(" ").append(order);
		}
		PagerEasyUI<WeixinMaterial> easyUIPager = new PagerEasyUI<WeixinMaterial>();
		List<WeixinMaterial> list = weixinMaterialDao.findByEasyUI(hql.toString(), page, rows);
		if (EmptyUtil.isNotEmpty(list)) {
			easyUIPager.setRows(list);
			easyUIPager.setTotal((Long) weixinMaterialDao.queryByHql(" select count(*) " + hql.substring(0, hql.indexOf("order by"))));
		}
		return easyUIPager;
	}

}
